package com.extsci.webmail.common.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * @author ccoke
 */
public class TimeUtils {
    /**
     * 将时间戳转换为日期
     */
    public static Date timestrampToDate(long timestrampL) {
        Instant instant = Instant.ofEpochMilli(timestrampL);
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * 将日期转换为时间戳
     */
    public static long dateToTimestramp(Date date) {
        return date.getTime();
    }

    /**
     * 获取比当前时间早*小时的时间
     */
    public static long getTimeBeforeCurrent(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -hour);
        return calendar.getTimeInMillis();
    }
}
